package com.lili.str;

/**
 * @Auther: 李 力
 * @Date: 2024/8/22
 * @Description: 找出字符串中第一个匹配项的下标
 * @version: 1.0
 */
public class LeetCode28 {

    public static int strStr(String haystack, String needle) {
        char[] charArray1 = haystack.toCharArray();
        char[] charArray2 = needle.toCharArray();
        int i = 0, j = 0;
        while (i <= charArray1.length - charArray2.length) {
            for (j = 0; j < charArray2.length; j++) {
                if (charArray2[j] != charArray1[i + j]) {
                    break;
                }
            }
            if (j == charArray2.length) {
                return i;
            }
            i++;
        }
        return -1;
    }
}
